package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @Override
    @Transactional
    public boolean sava(DishDto dishDto) {
        //1.完善菜品数据
        //(修改时间,添加时间,sort默认为0,是否禁售设置为1起售)
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);

        //2.调用数据访问层添加菜品,要求获取到添加菜品主键id
        dishMapper.insert(dishDto);

        //3.遍历口味记录列表完善数据
        List<DishFlavor> dishFlavorList=dishDto.getFlavors().stream().map(dishFlavor -> {
            //dishFlavor 是获取集合中的每个元素对象
            //(设置菜品的主键 更新时间 创建时间 更新人 创建人)
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            return dishFlavor;
        }).collect(Collectors.toList());
        //4.调用数据访问层插入多条口味记录数据
        dishFlavorMapper.save(dishFlavorList);

        return true;
    }

    /**
     * 分页查询(连表查询)
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<DishDto> dtoList = dishMapper.findAll(name);
        PageInfo<DishDto> pageInfo = new PageInfo<>(dtoList);

        Page<DishDto> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    /**
     * 根据id查询菜品和多个口味(修改回显数据)
     *
     * @param id
     * @return
     */
    @Override
    public DishDto findById(Long id) {
       return dishMapper.findById(id);
    }

    /**
     * 修改菜品
     *
     * @param dishDto 页面传递过来的参数包含菜品和口味
     * @return
     */
    @Override
    @Transactional
    public boolean update(DishDto dishDto) {
        //1.补全修改时间
        dishDto.setUpdateTime(LocalDateTime.now());

        //2.修改菜品
        dishMapper.update(dishDto);

        //3.删除该菜品的所有口味信息
        dishFlavorMapper.deleteByDishId(dishDto.getId());

        //4.补全口味信息,重新插入
        List<DishFlavor> flavors=dishDto.getFlavors();
        List<DishFlavor> dishFlavorList=flavors.stream().map((flavor -> {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());

            return flavor;
        })).collect(Collectors.toList());

        //4.保存口味信息
        return dishFlavorMapper.save(dishFlavorList)>0;
    }

    /**
     * 根据id查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        return dishMapper.findByCategoryId(categoryId);
    }

    /**
     * 根据类别和状态查询菜品列表List<DishDto>
     *
     * @param categoryId 类别id
     * @param status     状态
     * @return List
     */
    @Override
    public List<DishDto> findByCategoryId(Long categoryId, Integer status) {
        //1.调用mapper根据类别id和状态查询菜品列表
        List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status);
        //2.加工处理dishList封装到List<DishDto>,完善里面口味列表数据
        List<DishDto> dishDtoList=dishList.stream().map((dish -> {
            //实例DishDto对象封装数据
            DishDto dishDto = new DishDto();

            //将dish对象所有数据赋值给dishDto对象
            BeanUtils.copyProperties(dish,dishDto);
            //根据菜品id获取口味列表数据封装到dishDto里面
            List<DishFlavor> dishFlavorList=dishFlavorMapper.findByDishId(dishDto.getId());
            dishDto.setFlavors(dishFlavorList);

            //返回dto对象
            return dishDto;
        })).collect(Collectors.toList());

        //返回数据
        return dishDtoList;
    }
}
